﻿using System;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Taxonomy;
using PIT.Labs.ProjectRepository.Owl2DotNet;
using PIT.Labs.ProjectRepository.Owl2DotNet.Rdf;
using System.Globalization;

namespace PIT.Labs.ProjectRepository.OntologyFeature.OntologyUploader
{
    public partial class OntologyUploaderUserControl : UserControl
    {
        static readonly XNamespace owl = "http://www.w3.org/2002/07/owl#";
        static readonly XNamespace rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
        static readonly XNamespace rdfs = "http://www.w3.org/2000/01/rdf-schema#";

        static readonly XName xOntologyInfo = owl + "Ontology";
        static readonly XName xVersionInfo = owl + "versionInfo";
        static readonly XName xComment = rdfs + "comment";

        static readonly SPSite spSite = SPContext.Current.Site;

        /// <summary>
        /// Create new ontology uploader user control
        /// </summary>
        public OntologyUploaderUserControl()
        {
            // Set AllowUnsafeUpdates = true to avoid security error                       
            spSite.OpenWeb().AllowUnsafeUpdates = true;
        }

        // TODO: uncommented empty function, check if not needed
        //protected void Page_Load(object sender, EventArgs e)
        //{
        //}

        private static IOntology StoreOntologyFile(String filename, System.IO.Stream strm)
        {
            SPWeb spWeb = spSite.OpenWeb();
            SPFolder spLibrary = spWeb.Folders["OntologyXMLFiles"];
            SPFile addedFile = spLibrary.Files.Add(filename, strm, true);

            addedFile.Update();

            OntologyHelper.ClearOntology(addedFile.UniqueId);
            IOntology ontology = OntologyHelper.GetOntology(addedFile.UniqueId);
            
            string documentVersion = ontology.Annotations.First(x => x.Key.Label=="versionInfo").Value.Value;
            string documentComment = ontology.Annotations.First(x => x.Key.Label=="comment").Value.Value;

            addedFile.Properties["Description0"] = String.Format(CultureInfo.InvariantCulture, "Desc: {0} \r\n {1}", documentVersion, documentComment);

            addedFile.Update();

            return ontology;
        }

        /// <summary>
        /// Event Handler for button click. Will save the posted ontology file in the OntologyXMLFiles list. 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (FileUpload1.PostedFile != null)
            {
                if (FileUpload1.PostedFile.ContentLength > 0)
                {
                    StoreOntologyFile(System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName), FileUpload1.PostedFile.InputStream);
                }
            }
        }
    }
}
